package databasetest;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDesktopPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

public class DatabaseTest extends JFrame {
 //우리은규 우리은규 재내은규 은규사마 은규 미르메 미르메 데스네~ 쏘까~ 까치~ 치질~ 
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user = "hr";
	String db_pwd = "hr";
	Connection con;
	
	Insert insert;
	Update update;
	Delete delete;
	Select select;
	
	JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DatabaseTest frame = new DatabaseTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DatabaseTest() {
		setTitle("데이타베이스 연습");
		setBounds(100, 100, 1000, 700);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		//데이타베이스 연결 테스트
		//1) 드라이버 로딩
		try {
			//newInstance 이게 있으면 창이 열릴때마다 새로 드라이버가 만들어짐?
			Class.forName(driver).newInstance();
			System.out.println("드라이버가 로딩되었습니다.");
			
			//2)Connection 객체 생성
			con = DriverManager.getConnection(url, db_user, db_pwd);
			System.out.println("데이타베이스가 정상적으로 연결됨");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("메뉴");
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem_2());
			mnNewMenu.add(getMntmNewMenuItem_3());
			mnNewMenu.add(getMntmNewMenuItem());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("조회");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openSelect();
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("입력");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openInsert();
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("수정");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openUpdate();
				}
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("삭제");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openDelete();
				}
			});
		}
		return mntmNewMenuItem_3;
	}
	
	public void openInsert(){
		if(insert == null){
			insert = new Insert(con,this);
			contentPane.add(insert);
		}
		//defaultCloseOperation 을 HIDE_ON_CLOSE
		insert.setVisible(true);
		insert.toFront();
	}
	
	public void openUpdate(){
		if(update == null){
			update = new Update(con,this);
		}
		if(update.isVisible() == false){
			contentPane.add(update);
		}
		update.setVisible(true);
		update.toFront();
	}
	
	public void openDelete(){
		if(delete == null){
			delete = new Delete(con,this);
		}
		if(delete.isVisible() == false){
			contentPane.add(delete);
		}
		delete.setVisible(true);
		//delete.toFront();
	}
	
	public void openSelect(){
		if(select == null){
			select = new Select(con,this);
		}
		if(select.isVisible() == false){
			contentPane.add(select);
		}
		select.setVisible(true);
		//select.toFront();
	}
	
}
